package cn.com.infohold.smartcity.activity.Approve;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.reflect.TypeToken;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.lang.reflect.Type;
import java.util.List;
import java.util.Map;

import cn.com.infohold.smartcity.R;
import cn.com.infohold.smartcity.api.HttpEvent;
import cn.com.infohold.smartcity.api.SDK;
import cn.com.infohold.smartcity.api.bean.ApproveBean;
import cn.com.infohold.smartcity.api.bean.LandResult;
import cn.com.infohold.smartcity.api.bean.ListResult;
import cn.com.infohold.smartcity.api.bean.WarrantBean;
import cn.com.infohold.smartcity.data.XmlData;
import cn.com.infohold.smartcity.parent.ParentActivity;
import cn.com.infohold.smartcity.utils.GsonUtil;
import cn.com.infohold.smartcity.utils.RequestCode;
import common.annotation.ViewInject;

/**
 * Created by Monsoon on 2017/11/8.
 */
public class ApproveDetail_Activity extends ParentActivity{

    @ViewInject(R.id.ib_back)
    ImageButton ib_back;
    @ViewInject(R.id.tv_docnumber)
    TextView tv_docnumber;
    @ViewInject(R.id.tv_contracttype)
    TextView tv_contracttype;
    @ViewInject(R.id.tv_contractnumber)
    TextView tv_contractnumber;
    @ViewInject(R.id.tv_responsible_person)
    TextView tv_responsible_person;
    @ViewInject(R.id.tv_responsible_name)
    TextView tv_responsible_name;
    @ViewInject(R.id.tv_area)
    TextView tv_area;
    @ViewInject(R.id.tv_certificatedate)
    TextView tv_certificatedate;
    @ViewInject(R.id.tv_startdate)
    TextView tv_startdate;
    @ViewInject(R.id.tv_enddate)
    TextView tv_enddate;
    @ViewInject(R.id.tv_fee)
    TextView tv_fee;
    @ViewInject(R.id.tv_payway)
    TextView tv_payway;
    @ViewInject(R.id.tv_disputetype)
    TextView tv_disputetype;
    @ViewInject(R.id.tv_reason)
    TextView tv_reason;
    @ViewInject(R.id.tv_remark)
    TextView tv_remark;
    @ViewInject(R.id.et_opinion)
    EditText et_opinion;

    @ViewInject(R.id.ll_approve)
    LinearLayout ll_approve;
    @ViewInject(R.id.ll_bottom)
    LinearLayout ll_bottom;
    @ViewInject(R.id.btn_confirm)
    Button btn_confirm;
    @ViewInject(R.id.btn_cancle)
    Button btn_cancle;

    private WarrantBean warrantBean;
    private ApproveBean approveBean;

    @Override
    protected Object inflateView() {
        return R.layout.activity_approve_detail;
    }

    @Override
    protected void initView(View anchor) {

        approveBean = new ApproveBean();

        Intent intent = getIntent();
        Bundle approve_detail = intent.getBundleExtra("approve_detail");
        if (approve_detail != null){
            warrantBean = (WarrantBean) approve_detail.getSerializable("approve_detail");
            boolean isApprove = approve_detail.getBoolean("isApprove");
            if (isApprove){
                ll_bottom.setVisibility(View.GONE);
                ll_approve.setVisibility(View.GONE);
            }else {
                if (XmlData.getInstance().getUserInfo().getRoles().equals("4")){
                    ll_bottom.setVisibility(View.GONE);
                    ll_approve.setVisibility(View.GONE);
                }else {
                    ll_bottom.setVisibility(View.VISIBLE);
                    ll_approve.setVisibility(View.VISIBLE);
                }
            }

            tv_docnumber.setText(warrantBean.getDocnumber());
            tv_contracttype.setText(warrantBean.getContracttype());
            tv_contractnumber.setText(warrantBean.getContractnumber());
            tv_area.setText(warrantBean.getArea() + "");
            tv_certificatedate.setText(warrantBean.getCertificatedate());
            tv_startdate.setText(warrantBean.getStartdate());
            tv_enddate.setText(warrantBean.getEnddate());
            tv_disputetype.setText(warrantBean.getDisputetype());
            tv_remark.setText(warrantBean.getRemark());
        }

        btn_confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                approveBean.setMode("warrant");
                approveBean.setApprover(XmlData.getInstance().getUserInfo().getId());
                approveBean.setRemark(et_opinion.getText().toString());
                switch (warrantBean.getStatus()){
                    case 1:
                        approveBean.setResult("2");
                        break;
                    case 2:
                        approveBean.setResult("5");
                        break;
                    case 5:
                        approveBean.setResult("8");
                        break;
                    default:
                        break;
                }

                approveBean.setModeid(warrantBean.getId());

                initData();
            }
        });

        btn_cancle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                approveBean.setMode("warrant");
                approveBean.setApprover(XmlData.getInstance().getUserInfo().getId());
                approveBean.setRemark(et_opinion.getText().toString());
                switch (warrantBean.getStatus()){
                    case 2:
                        approveBean.setResult("3");
                        break;
                    case 5:
                        approveBean.setResult("6");
                        break;
                    default:
                        break;
                }
                approveBean.setModeid(warrantBean.getId());

                initData();
            }
        });
    }

    private void initData(){
        SDK.api().approveData(approveBean).postEvent(RequestCode.REQUEST_CODE_APPROVE);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void getData(HttpEvent<String> httpEvent){

        int requestCode = httpEvent.getRequestCode();
        if (requestCode != RequestCode.REQUEST_CODE_APPROVE) {
            return;
        }

        if (httpEvent.getState() != HttpEvent.STATE_SUCCESS) {
            return;
        } else {
            switch (requestCode){
                case RequestCode.REQUEST_CODE_APPROVE:
                    Type type = new TypeToken<LandResult<Map<String,String>>>(){}.getType();
                    LandResult<Map<String,String>> result = GsonUtil.fromJson(httpEvent.getData(),type);
                    Toast.makeText(this, result.getMessage(), Toast.LENGTH_SHORT).show();
                    if (result.getStatus().equals("1")){
                        finish();
                        overridePendingTransitionFade();
                    }

                    break;
                default:
                    break;
            }
        }
    }

}
